package com.netflexitysolutions.amazonws.s3.internal.operations;

import java.util.List;

import com.amazonaws.s3.doc._2006_03_01.AccessControlPolicy;
import com.amazonaws.s3.doc._2006_03_01.GetBucketAccessControlPolicy;
import com.netflexitysolutions.amazonws.s3.S3;
import com.netflexitysolutions.amazonws.s3.S3AccessControlPolicy;
import com.netflexitysolutions.amazonws.s3.S3Grant;
import com.netflexitysolutions.amazonws.s3.S3Owner;
import com.netflexitysolutions.amazonws.s3.internal.utils.S3Utils;

public class GetBucketAccessControlPolicyOperation extends OperationExecutor<GetBucketAccessControlPolicy, S3AccessControlPolicy> {

    private String bucket;

    public GetBucketAccessControlPolicyOperation(S3 s3, String bucket) {
        super(s3);
        this.bucket = bucket;
    }

    @Override
    protected S3AccessControlPolicy call(GetBucketAccessControlPolicy request) {
        request.setBucket(bucket);
        injectCredentials(request, "GetBucketAccessControlPolicy", getS3().getAccessKey(), getS3().getSecretKey());
        AccessControlPolicy acp = getS3().getService().getBucketAccessControlPolicy(request).getGetBucketAccessControlPolicyResponse();
        List<S3Grant> acl = S3Utils.convert(acp.getAccessControlList());
        S3Owner owner = new S3Owner(acp.getOwner().getID(), acp.getOwner().getDisplayName());
        return new S3AccessControlPolicy(owner, acl);
    }
}

